﻿using System.Web.Mvc;
using Application.Services.PartnersManagement;
using Domain.Model.Partners;
using Presentation.Web.Client.Areas.PartnersManagement.Models;
using Presentation.Web.Client.Areas.PartnersManagement.Models.PartnerDetails;
using Presentation.Web.Client.Areas.SalesManagement.Models;
using Presentation.Web.Client.Models;

namespace Presentation.Web.Client.Areas.PartnersManagement.Controllers
{
    public class PartnersController : Controller
    {
        private readonly IPartnersManagementService _partnersService;

        public PartnersController(IPartnersManagementService partnersService)
        {
            _partnersService = partnersService;
        }

        [HttpGet]
        public ViewResult List()
        {
            var model = new PartnersListViewModel(_partnersService);
            return View(model);
        }

        [HttpGet]
        public ViewResult Create()
        {
            var partner = _partnersService.CreateNewPartner();
            return View("Details", partner);
        }

        [HttpGet]
        public ViewResult Edit(int id)
        {
            var partner = _partnersService.FindPartnerByCode(id);
            var model = new PartnerDetailsViewModel(partner);
            return View("Details", model);
        }

        [HttpPost]
        public ActionResult Edit(PartnerDetailsViewModel model, string tabName, string saveButton = null)
        {
            if (saveButton == "Save")
            {
                _partnersService.SavePartner(model.CurrentPartner);
                return RedirectToAction("List");
            }

            if (tabName != null)
            {
                model.ChangeCurrentTab(tabName);
                return View("Details", model);
            }

            return RedirectToAction("List");
        }

       
    }
}
